package com.yiqi.liebang.feature.mine.model;

import android.text.TextUtils;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.yiqi.liebang.entity.bo.CouponBo;
import com.yiqi.liebang.entity.bo.OrganizationBo;
import com.yiqi.liebang.entity.bo.PageBo;
import com.yiqi.liebang.feature.mine.contract.CouponContract;
import com.yiqi.liebang.feature.mine.contract.EnterpriseContract;
import com.yiqi.liebang.feature.mine.dependencies.coupon.DaggerCouponModelComponent;
import com.yiqi.liebang.feature.mine.dependencies.enterprise.DaggerEnterpriseModelComponent;
import com.yiqi.liebang.framework.api.ApiLib;
import com.yiqi.liebang.framework.api.UserApi;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class EnterpriseModel implements EnterpriseContract.Model {

    @Inject
    UserApi api;


    @Inject
    public EnterpriseModel() {
        DaggerEnterpriseModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<OrganizationBo> getorganizationCardData() {
        return api.getOrganizationInfo().compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> organizationCertification(
             String organizationName, String organizationFullName,String contactName,
            String contactPosition, String contactMobile, String contactEmail,
            String contactCity, String contactAddress, String businessLicense) {

        MultipartBody.Part cardFilePart = null;
        if (!TextUtils.isEmpty(businessLicense)) {
            File headFile = new File(businessLicense);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            cardFilePart = MultipartBody.Part.createFormData("license", headFile.getName(), requestFile);
        }

        RequestBody organizationFullNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), organizationFullName);
        RequestBody organizationNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), organizationName);
        RequestBody contactNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactName);
        RequestBody contactPositionBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactPosition);
        RequestBody contactMobileBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactMobile);
        RequestBody contactEmailBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactEmail);
        RequestBody contactCityBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactCity);
        RequestBody contactAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), contactAddress);


        return api.organizationCertification(
                organizationFullNameBody,
                organizationNameBody,
                contactNameBody,
                contactPositionBody,
                contactMobileBody,
                contactEmailBody,
                contactCityBody,
                contactAddressBody,
                cardFilePart
        ).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
